import axios from "axios";
// import { MessageBox, Notification } from 'element-ui';
import store from "@/store";
// import { getToken } from '@/utils/auth';
import qs from "qs";
import { message } from "ant-design-vue";
// import qs from 'qs';
// let isResetToken = false;

// create an axios instance
const service = axios.create({
  baseURL: import.meta.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 1000 * 60, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {
    if (config.method === "get") {
      config.paramsSerializer = (params) => {
        return qs.stringify(params, { arrayFormat: "repeat" });
      };
    }

    // if (store.getters.token) {
    //   // let each request carry token
    //   // please modify it according to the actual situation
    //   config.headers.Authorization = getToken();
    // }
    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  },
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data;
    if (res.code === 401 || res.code === 99999) {
      // if (isResetToken) return;
      // isResetToken = true;
      // MessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
      //   confirmButtonText: '重新登录',
      //   cancelButtonText: '取消',
      //   type: 'warning',
      // }).then(() => {
      //   store.dispatch('user/resetToken').then(() => {
      //     isResetToken = false;
      //     location.reload();
      //   });
      // });
      message.error("接口请求有问题啦");
    } else if (res.code === 200 || res.code === 200020) {
      message.success("接口请求成功");
      return res;
    } else {
      message.error("接口请求有问题啦");
      return Promise.reject("error");
    }
  },
  (error) => {
    Notification.error({
      title: "服务器内部错误，请联系管理员",
    });
    return Promise.reject(error);
  },
);

export default service;
